__author__ = 'vincent LIU'

import pandas as pd
import os

tester_path = "E:\\SHXT\\coHR\\第六批\\ZhouJinyu\\MyTest\\MyTest\\bin\\Debug\\"
#tester_path = "Z:\\personal\\qxli\\Test\\answer\\"
answer_path = "Z:\\personal\\yliu\\test\\"

output_path = "Z:\\personal\\test\\"
output_fd = ""

def LoadOutputQ1(path):
	df = pd.read_csv(path,index_col =0)
	return df

def LoadOutputQ3(path):
	col_header = ["#Ticker","Date","High","Low"]
	df = pd.read_csv(path)
	df.columns = col_header
	df=df.set_index(['#Ticker','Date'])
	return df

def LoadOutputQ4(path):
	df = pd.read_csv(path,index_col=0)
	# df=df.T.T
	return df

def ReformatDf(df1,df2):
	idx_list = df2.index.tolist()
	if isinstance(idx_list[0],str) :
		for i in range(len(idx_list)):
			#idx_list[i] = idx_list[i].strip("/\\'-")
			idx_list[i]=idx_list[i].replace("-","")
			idx_list[i]=idx_list[i].replace("'","")
			idx_list[i]=idx_list[i].replace("\\","")
			idx_list[i]=idx_list[i].replace("/","")
			if isinstance(idx_list[i],str) :
				if idx_list[i].isdigit():
					idx_list[i]=int(idx_list[i])
				else:
					df2.drop(idx_list[i],inplace=True)
					del idx_list[i]
					pass
		df2 = df2.T
		df2.columns=idx_list
		df2 = df2.T
	setA=set(df1.index.tolist())
	setB=set(df2.index.tolist())
	lack_idx_set = setA.difference(setB)
	redundancy_idx_set = setB.difference(setA)
	colA = set(df1.columns.tolist())
	colB = set(df2.columns.tolist())
	right_rowcounts = len(setA)
	right_colcounts = len(colA)
	lack_col_set = colA.difference(colB)
	redundancy_col_set = colB.difference(colA)
	lack_idx_cnt = len(lack_idx_set)
	redundancy_idx_cnt = len(redundancy_idx_set)
	lack_col_cnt = len(lack_col_set)
	redundancy_col_cnt = len(redundancy_col_set)
	return (colA,colB,setA,setB,right_rowcounts,right_colcounts,lack_col_cnt,lack_idx_cnt,redundancy_col_cnt,redundancy_idx_cnt,df2)


def CompareOutput(df1,df2):
	(colA,colB,setA,setB,right_rowcounts,right_colcounts,lack_col_cnt,lack_idx_cnt,redundancy_col_cnt,
	 redundancy_idx_cnt,df2) = ReformatDf(df1,df2)
	'''judge if the data is reversed. we help them restore and compare'''
	if abs(right_rowcounts - len(colB))<=1 or abs(right_colcounts - len(setB))<=1:
		output_fd.write("[Warning]The data is reversed.\n")
		df2 = df2.T
		(colA,colB,setA,setB,right_rowcounts,right_colcounts,lack_col_cnt,lack_idx_cnt,redundancy_col_cnt,
	 redundancy_idx_cnt,df2) = ReformatDf(df1,df2)
	'''judge the ticker's format '''
	if right_colcounts == len(colB) and lack_col_cnt > 100:
		output_fd.write("[Warning]The ticker's format is wrong.\n")
		col_list = df2.columns.tolist()
		df2.columns = ['{0:>06}'.format(x) for x in col_list]
		(colA,colB,setA,setB,right_rowcounts,right_colcounts,lack_col_cnt,lack_idx_cnt,redundancy_col_cnt,
	 redundancy_idx_cnt,df2) = ReformatDf(df1,df2)
	'''judge if the data keep the precision to be 4 decimal digits'''
	#TODO
	num_wrong = lack_col_cnt * right_rowcounts + lack_idx_cnt*right_colcounts
	output_fd.write("[Info]Lack rows:%d\n" % lack_idx_cnt)
	output_fd.write("[Info]Redundancy rows:%d\n" % (redundancy_idx_cnt))
	output_fd.write("[Info]Lack columns:%d\n" % (lack_col_cnt))
	output_fd.write("[Info]Redundancy columns:%d\n" % redundancy_col_cnt)
	if len(colA) == 0:
		return
	inter_row = setB.intersection(setA)
	inter_col = colB.intersection(colA)
	inter_row_cnt =len(inter_row)
	inter_col_cnt = len(inter_col)
	output_fd.write("[Info]Interaction columns:%d\n" % (inter_col_cnt))
	output_fd.write("[Info]Interaction rows:%d\n" % inter_row_cnt)
	if inter_row_cnt!=0 and inter_col_cnt!=0:
		df2 = df2.loc[list(inter_row),list(inter_col)]
		#df2=df2.to_frame()
	else:
		output_fd.write("[Wrong]There's no interaction ouput.\n")
		return
	calc_ret = df1 - df2
	#output_fd.write(calc_ret)
	calc_ret.fillna(1,inplace=True)#1 means wrong
	ret_df = calc_ret.apply(lambda x:x<=0.0002)
	#output_fd.write(ret_df)
	all_num = len(colA) * len(setA)
	inter_cnt = 0#interaction number
	wrong_cnt=0
	for col in ret_df.columns:
		col_cnt = ret_df[col].value_counts()
		if False in col_cnt:
			wrong_cnt += col_cnt[False]
			#output_fd.write(col,col_cnt)
	output_fd.write("[Info]Wrong answer number:%d\n" % wrong_cnt)
	#output_fd.write("[Info]Answer all number:%d" % all_num)
	#output_fd.write("The correct percent for interaction part:%.2f%%" % ((all_num-cnt)*100/all_num))
	output_fd.write("[Info]The correct percent finally:%.2f%%\n" % ((all_num - wrong_cnt)*100/all_num))

def CheckOutputFile(path):
	if os.path.exists(path):
		return True
	else:
		output_fd.write("No output file.\n")
		return False

def CheckQ1(tname):
	output_fd.write("------------q1 checking result-------------\n")
	q_num = "Q1"
	t_q1 = tester_path + tname + "\\output\\"+q_num+".output.csv"
	ans_q1 = answer_path + q_num + "\\"+ q_num + ".output.csv"
	if(CheckOutputFile(t_q1)==False):
		return
	df1 = LoadOutputQ1(ans_q1)
	df2 = LoadOutputQ1(t_q1)
	CompareOutput(df1,df2)

def CheckQ2(tname):
	output_fd.write("------------q2 checking result-------------\n")
	q_num = "Q2"
	t_q2 = tester_path + tname + "\\output\\"+q_num+".output.csv"
	ans_q2 = answer_path + q_num + "\\"+ q_num + ".output.csv"
	if(CheckOutputFile(t_q2)==False):
		return
	df1 = LoadOutputQ1(ans_q2)
	df2 = LoadOutputQ1(t_q2)
	CompareOutput(df1,df2)

def CheckQ3(tname):
	output_fd.write("------------q3 checking result-------------\n")
	q_num = "Q3"
	t_q3 = tester_path + tname + "\\output\\"+q_num+".output.csv"
	ans_q3 = answer_path + q_num + "\\"+ q_num + ".output.csv"
	if(CheckOutputFile(t_q3)==False):
		return
	df1 = LoadOutputQ3(ans_q3)
	df2 = LoadOutputQ3(t_q3)
	CompareOutput(df1,df2)

def CheckQ4(tname):
	try:
		output_fd.write("------------q4 checking result-------------\n")
		q_num = "Q4"
		t_q4 = tester_path + tname + "\\output\\"+q_num+".output.csv"
		ans_q4 = answer_path + q_num + "\\"+ q_num + ".output.csv"
		if(CheckOutputFile(t_q4)==False):
			return
		df1 = LoadOutputQ4(ans_q4)
		df2 = LoadOutputQ4(t_q4)
		CompareOutput(df1,df2)
	except Exception as e:
		output_fd.write("[Error]format error.error detail:{0}\n".format(e))
		
def OpenOuput(tname):
	global output_fd
	oname = output_path + tname + ".txt"
	output_fd = open(oname,mode='w+')

if __name__ == "__main__":
	tname = "ZhouJinYu"
	OpenOuput(tname)
	CheckQ1(tname)
	CheckQ2(tname)
	CheckQ3(tname)
	CheckQ4(tname)
	
	output_fd.close()


